package org.wwb.antbox.user.service.rolemenu;

import org.ant.commons.commons.log.Log;
import org.ant.commons.commons.log.LogFactory;
import org.ant.commons.models.result.BaseResult;
import org.ant.mybatis.query.Query;
import org.ant.service.common.BaseService;
import org.apache.dubbo.config.annotation.Service;
import org.wwb.antbox.user.data.rolemenu.SysRoleMenuMapper;
import org.wwb.antbox.user.iservice.rolemenu.ISysRoleMenuService;
import org.wwb.antbox.user.model.rolemenu.entity.SysRoleMenu;
import org.wwb.antbox.user.model.rolemenu.model.SysRoleMenuAddModel;
import org.wwb.antbox.user.model.rolemenu.model.SysRoleMenuDelModel;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Bond
 * @Date: 2020/2/23 10:51
 * @Description: 角色菜单关系Service实现
 */
@Service(registry = {"registryConfigUser"},provider ="dubboProviderUser")
public class SysRoleMenuServiceImpl extends BaseService<SysRoleMenuMapper, SysRoleMenu, String> implements ISysRoleMenuService {

    private static final Log log = LogFactory.getLog(SysRoleMenuServiceImpl.class);

    @Override
    public BaseResult<List<String>> getRoleMenuIdList(String roleId) {
        Query query = new Query().eq("role_id",roleId);
        List<SysRoleMenu> sysRoleMenuList = mapper.list(query);

        /**
         * 返回的Model
         * List<String>：菜单编号集合
         */
        List<String> returnModel = new ArrayList<>(sysRoleMenuList.size());

        for (SysRoleMenu sysRoleMenu : sysRoleMenuList){
            String menuId = sysRoleMenu.getMenuId();
            returnModel.add(menuId);
        }

        BaseResult<List<String>> baseResult = new BaseResult<>();
        baseResult.setModel(returnModel);
        baseResult.setSuccess(true);
        return baseResult;
    }

    @Override
    public void saveSysRoleMenu(SysRoleMenuAddModel sysRoleMenuAddModel) {

        // 角色菜单对象集合
        List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();


        // 菜单编号集合
        List<String> menuIds = sysRoleMenuAddModel.getMenuIds();

        if(!menuIds.isEmpty()){
            for(String menuId : menuIds){
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(sysRoleMenuAddModel.getRoleId());
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenuList.add(sysRoleMenu);
            }
        }
        if(!sysRoleMenuList.isEmpty()){
            mapper.saveBatch(sysRoleMenuList);
        }
    }

    @Override
    public BaseResult<Integer> delSysRoleMenu(SysRoleMenuDelModel sysRoleMenuDelModel) {
        Query query = new Query().in("role_id",sysRoleMenuDelModel.getRoleIds());
        int i = mapper.deleteByQuery(query);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(i);
        baseResult.setSuccess(i == 1);
        return baseResult;
    }


    @Override
    public void delSysRoleMenuByRoleIdAndMenuIds(String roleId, List<String> menuIds) {
        Query query = new Query().eq("role_id",roleId).in("menu_id",menuIds);;
        mapper.deleteByQuery(query);
    }
}
